package cn.lijiajia3515.cairo.auth;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Optional;

/**
 * 认证服务器 配置
 */
@Data
@Accessors(chain = true)
public class CairoAuthProperties {
	private static final String ISSUER_URI = "/";
	private static final String JWK_SET_URI = "/oauth2/jwks";
	private static final String AUTHORIZATION_URI = "/oauth2/authorize";
	private static final String ACCESS_TOKEN_URI = "/oauth2/token";
	private static final String REFRESH_TOKEN_URI = "/oauth2/token";
	private static final String AUTHENTICATION_URI = "/api/authentication";
	private static final String ACCOUNT_URI = "/api/account";

	/**
	 * 基础地址
	 */
	private String baseUri;

	/**
	 * issuer 地址
	 */
	private String issuerUri;

	/**
	 * jwk set 地址
	 */
	private String jwkSetUri;

	/**
	 * JSON Web Algorithm used for verifying the digital signatures.
	 */
	private String jwsAlgorithm = "RS256";

	/**
	 * 授权地址
	 */
	private String authorizationUrl;

	/**
	 * 访问令牌请求地址
	 */
	private String accessTokenUrl;

	/**
	 * 刷新令牌请求地址
	 */
	private String refreshTokenUrl;

	/**
	 * 凭证信息 (账号+权限)
	 */
	private String authenticationUrl;

	/**
	 * 账号信息
	 */
	private String accountUrl;

	public String getBaseUri() {
		return this.baseUri;
	}

	public String getIssuerUri() {
		return Optional.ofNullable(issuerUri).orElse(url(ISSUER_URI));
	}

	public String getJwkSetUri() {
		return Optional.ofNullable(jwkSetUri).orElse(url(JWK_SET_URI));
	}

	public String getAuthorizationUrl() {
		return Optional.ofNullable(authorizationUrl).orElse(url(AUTHORIZATION_URI));
	}

	public String getAccessTokenUrl() {
		return Optional.ofNullable(accessTokenUrl).orElse(url(ACCESS_TOKEN_URI));
	}

	public String getRefreshTokenUrl() {
		return Optional.ofNullable(refreshTokenUrl).orElse(url(REFRESH_TOKEN_URI));
	}


	public String getAuthenticationUrl() {
		return Optional.ofNullable(authenticationUrl).orElse(url(AUTHENTICATION_URI));
	}

	public String getAccountUrl() {
		return Optional.ofNullable(accountUrl).orElse(url(ACCOUNT_URI));
	}

	private String url(String uri) {
		return Optional.ofNullable(getBaseUri()).map(x -> x.concat(uri)).orElse(null);
	}

	public void setBaseUri(String baseUri) {
		this.baseUri = baseUri;
	}

	public void setIssuerUri(String issuerUri) {
		this.issuerUri = issuerUri;
	}

	public void setJwkSetUri(String jwkSetUri) {
		this.jwkSetUri = jwkSetUri;
	}

	public void setAuthorizationUrl(String authorizationUrl) {
		this.authorizationUrl = authorizationUrl;
	}

	public void setAccessTokenUrl(String accessTokenUrl) {
		this.accessTokenUrl = accessTokenUrl;
	}

	public void setRefreshTokenUrl(String refreshTokenUrl) {
		this.refreshTokenUrl = refreshTokenUrl;
	}

	public void setAuthenticationUrl(String authenticationUrl) {
		this.authenticationUrl = authenticationUrl;
	}

	public void setAccountUrl(String accountUrl) {
		this.accountUrl = accountUrl;
	}


}
